package com.zhuhjay.chat.client.handler;

import com.zhuhjay.chat.message.*;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;

import java.util.Arrays;
import java.util.Scanner;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * 控制台输入消息处理器
 * @author ZhuHJay
 * @date 2023/3/9 11:42
 */
public class ConsoleInputMessageHandler extends ChannelInboundHandlerAdapter {
    private final LoginResponseMessageHandler messageHandler;

    public ConsoleInputMessageHandler(LoginResponseMessageHandler messageHandler) {
        this.messageHandler = messageHandler;
    }

    @Override
    @SuppressWarnings("all")
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
        new Thread(() -> {
            Scanner sc = null;
            String username = null;
            while (!messageHandler.isLogin()) {
                sc = new Scanner(System.in);
                System.out.print("请输入用户名: ");
                username = sc.nextLine();
                System.out.print("请输入密码: ");
                String password = sc.nextLine();
                ctx.writeAndFlush(new LoginRequestMessage(username, password));
                try {
                    messageHandler.await();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            while (true) {
                System.out.println("================== 命令提示菜单 ====================");
                System.out.println("send [username] [content]");
                System.out.println("gsend [group name] [content]");
                System.out.println("gcreate [group name] [m1,m2,m3...]");
                System.out.println("gmembers [group name]");
                System.out.println("gjoin [group name]");
                System.out.println("gquit [group name]");
                System.out.println("quit");
                System.out.println("================== 命令提示菜单 ====================");
                System.out.print(">>> ");
                String command = sc.nextLine();
                String[] s = command.split(" ");
                switch (s[0]) {
                    case "send":
                        if (s.length < 3) {
                            System.out.printf("ERROR: 输入指令错误%n");
                            break;
                        }
                        ctx.writeAndFlush(new ChatRequestMessage(username, s[1], s[2]));
                        break;
                    case "gsend":
                        if (s.length < 3) {
                            System.out.printf("ERROR: 输入指令错误%n");
                            break;
                        }
                        ctx.writeAndFlush(new GroupChatRequestMessage(username, s[1], s[2]));
                        break;
                    case "gcreate":
                        if (s.length < 3) {
                            System.out.printf("ERROR: 输入指令错误%n");
                            break;
                        }
                        Set<String> members = Arrays.stream(s[2].split(",")).collect(Collectors.toSet());
                        // 将创建人添加进去
                        members.add(username);
                        ctx.writeAndFlush(new GroupCreateRequestMessage(s[1], members));
                        break;
                    case "gmembers":
                        if (s.length < 2) {
                            System.out.printf("ERROR: 输入指令错误%n");
                            break;
                        }
                        ctx.writeAndFlush(new GroupMembersRequestMessage(s[1]));
                        break;
                    case "gjoin":
                        if (s.length < 2) {
                            System.out.printf("ERROR: 输入指令错误%n");
                            break;
                        }
                        ctx.writeAndFlush(new GroupJoinRequestMessage(username, s[1]));
                        break;
                    case "gquit":
                        if (s.length < 2) {
                            System.out.printf("ERROR: 输入指令错误%n");
                        }
                        ctx.writeAndFlush(new GroupQuitRequestMessage(username, s[1]));
                        break;
                    case "quit":
                        ctx.channel().close();
                        break;
                    default:
                        System.out.println("输入错误！请重试");
                }
            }
        }, "input thread").start();
    }
}
